//
//  RequestViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/12/30.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "RequestViewController.h"
#import "DeviceHandle.h"
#import "RequestCell.h"
#import "RequestUnit.h"
#import "RequsetManager.h"

@interface RequestViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *resultArray;
}
@end

@implementation RequestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - NAVBARHEIGHT_STATUSBAR);
    
    self.tableView = [[UITableView alloc]initWithFrame:rect
                                                 style:UITableViewStylePlain];
    self.tableView.backgroundColor = MAIN_DARKCOLOR;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.titleLabel.text =@"请求消息";
    
    __weak typeof(self) weakSelf = self;
    
    
    NSString *device_id = @"";
    
//    __block  __weak __typeof(resultArray)weakV = resultArray;
    
    [self addFullloadView];
    [fullloadingView showLoadingState];
    
    [DeviceHandle deviceGetAllRequest:device_id
                              success:^() {
                                  
                                  NSArray*temp= [RequsetManager getLocalRequest:device_id];
                                  if(temp){
                                      resultArray = [NSMutableArray arrayWithArray:temp];
                                  }
                                  
                                  if(resultArray.count){
                                      [weakSelf dismissFullloadingView];
                                      [weakSelf.tableView reloadData];
                                  }else{
//                                      MBALERT(@"没有数据");
                                      [weakSelf showErrorTipMsgInViewController:@"没有数据"];
                                  }
                                      
   
                                  //        }
                              } failure:^(NSString *error) {
                                  
                                  NSArray*temp= [RequsetManager getLocalRequest:device_id];
                                  if(temp){
                                      resultArray = [NSMutableArray arrayWithArray:temp];
                                  }
                                  
                                  if(resultArray.count){
                                      [weakSelf.tableView reloadData];
                                  }else{
                                      [self showErrorTipMsgInViewController:@"没有数据"];
                                  }
                                  
                                  [weakSelf.tableView reloadData];
                                  
                                  
                              }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return resultArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 73;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    __weak typeof(self) weakSelf = self;
    RequestUnit *request = resultArray[indexPath.row];

    RequestCell *cell = [RequestCell cellWithTableView:tableView];
    cell.tapBlock = ^(){
        
        UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
        hud=[MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.labelText=@"正在处理...";
    
        //请求同意
        [DeviceHandle deviceAcceptSharerRequest:request.device_id userid:request.requester_phoneno success:^{
            
            hud.mode = MBProgressHUDModeText;
            hud.labelText=@"已同意该请求";
            [hud hide:YES afterDelay:0.5];
            
            request.hasAgree = YES;

            //成功
            [weakSelf.tableView reloadData];

            //保存数据
            [RequsetManager  resetLocalRequest:@"" request:resultArray];
            
        } failure:^(NSString *error) {
            //失败
            hud.mode = MBProgressHUDModeText;
            hud.labelText=error;
            [hud hide:YES afterDelay:0.5];

        }];
    };
    
    cell.requestUnit = request;

    cell.rowType=3;
    
    
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)atableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //先删除本地数据
        NSString *device_id = @"";
        [resultArray removeObjectAtIndex:indexPath.row];
        [RequsetManager resetLocalRequest:device_id request:resultArray];
        
        [atableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    RequestUnit *request = resultArray[indexPath.row];
//    user.selected = !user.selected;
//
//    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
